﻿using System;
using System.IO;
using System.Data;
using System.Windows;
using System.Collections;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Controls;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Collections.Generic;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;

namespace Sinacor.UI.Bovespa.Movimento.View.SmartClient
{
    public partial class ConsultaAssessores : SinacorWindow, Presenter.IConsultaAssessor
    {
        #region Atributos

        private Presenter.ConsultaAssessor _presenterConsultaAssessor = null;

        #endregion

        #region Construtor

        public ConsultaAssessores()
        {
            InitializeComponent();

            _presenterConsultaAssessor = new Presenter.ConsultaAssessor(this);

            this.DataContext = PresenterConsultaAssessor;

            DtgAssessores.NewItemType = typeof(Model.ConsultaAssessor);

            this.AfterInitialize += new RoutedEventHandler(ConsultaAssessores_AfterInitialize);
            BtnPesquisar.Click += new RoutedEventHandler(BtnPesquisar_Click);
            BtnNovaPesquisa.Click += new RoutedEventHandler(BtnNovaPesquisa_Click);
            CboAssessor.SelectionChanged += new SelectionChangedEventHandler(CboAssessor_SelectionChanged);
            CboCorrespondente.SelectionChanged += new SelectionChangedEventHandler(CboCorrespondente_SelectionChanged);
        }         

        #endregion

        #region Propriedades

        public Presenter.ConsultaAssessor PresenterConsultaAssessor
        {
            get { return _presenterConsultaAssessor; }
            set { _presenterConsultaAssessor = value; }
        }

        #endregion

        #region Event Handlers

        private void ConsultaAssessores_AfterInitialize(object sender, RoutedEventArgs e)
        {
            PresenterConsultaAssessor.Inicializar();
        }

        private void BtnNovaPesquisa_Click(object sender, RoutedEventArgs e)
        {
            LimparCampos(ExpPesquisa);
        }

        private void BtnPesquisar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ClearServiceErrors();

                //Força o binding
                this.MoveFocus(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.Next));
                this.MoveFocus(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.Previous));

                ShowUserWaitingProcess();

                _presenterConsultaAssessor.ConsultarMovimentoAssessores();
            }
            finally
            {
                HideUserWaitingProcess();
            }
        }

        private void CboAssessor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PresenterConsultaAssessor.ConsultarAgentes();

            if (CboAssessor.SelectedIndex != -1)
            {
                CboCorrespondente.SelectedIndex = -1;
            }
        }

        private void CboCorrespondente_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CboCorrespondente.SelectedIndex != -1)
            {
                CboAssessor.SelectedIndex = -1;
                CboAgente.SelectedIndex = -1;
            }
        }       

        #endregion

        #region IConsultaAssessor Members

        public void AtualizarAgentes()
        {
            CboAgente.UpdateTarget(SinacorComboBox.ItemsSourceProperty);

            CboAgente.IsEnabled = (CboAssessor.SelectedIndex != -1);
        }

        public void AtualizarAssessores()
        {
            CboAssessor.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        public void AtualizarCorrespondentes()
        {
            CboCorrespondente.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        public void AtualizarMovimentoAssessores()
        {
            DtgAssessores.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        public void InformarPesquisaSemResultado()
        {
            SinacorMessageBox.Show(this.FindResource("Bovespa.Movimento.Message.PesquisaSemRetorno").ToString(),
                                   SinacorMessageBoxButton.Ok,
                                   SinacorMessageBoxImage.Exclamation);
        }

        #endregion
    }
}